//
//  HDSMPenSettingVC.m
//  HDSmartPenSDKDemo
//
//  Created by 牛的mini on 2016/12/26.
//  Copyright © 2016年 牛的mini. All rights reserved.
//

#import "HDSMPenSettingVC.h"
#import "HDSyncDataVC.h"
@interface HDSMPenSettingVC ()<UITableViewDataSource,UITableViewDelegate>
{
    UIButton *rightButton;
}
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *iconArray;
@property (nonatomic, strong) NSArray *lineArray;
@property (nonatomic, strong) HDBeaconManager *beaconManager;
@property (nonatomic, strong) NSTimer *heartBeatTimer;

@end

@implementation HDSMPenSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _beaconManager = [HDBeaconManager sharedBeacon];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _myTableView.rowHeight = 50.0f;
    
    self.title = @"智慧笔设置";
    _dataArray = @[@"查找智慧笔",@"智慧笔加密配对",@"硬件配置",@"参数设置",@"硬件信息",@"音量设置",@"工程模式",@"NFC标签调试",@"MP3播放测试",@"同步数据"];
    _iconArray = @[@"control_look",@"control_pair",@"control_hard",@"control_set",@"control_hard_set",@"control_voice",@"control_debug",@"control_nfc",@"control_mp3",@"control_mp3"];
    _lineArray = @[@"",
                   @"",
                   @"HDSMPenSettingVCPushToSwitchLine",
                   @"HDSMPenSettingVCPushToILanguageLine",
                   @"HDSMPenSettingVCPushToInfoLine",
                   @"HDSMPenSettingVCPushToVolumLine",
                   @"HDSMPenSettingVCPushToDebugLine",
                   @"HDSMPenSettingVCPushToNFCLine",
                   @"",
                   @""];
    [self rightBtnInit];
    [_beaconManager searchSMPenIDAction]; // 获取ID
}

- (void)rightBtnInit
{
    rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0.0, 40, 40);
    [rightButton setImage:[UIImage imageNamed:@"control_collect"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(connectToSMPenAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:rightButton];
    [self.navigationItem setRightBarButtonItem:rightBarButtonItem];
}

- (void)connectToSMPenAction
{
    PeripheralModel *l_peri = [[HDDeclare sharedDeclare] peripheralObj];
    if ([[HDDeclare sharedDeclare] isConnected] == YES)
    {
        [_beaconManager disconnectPeripheral:l_peri];
        [rightButton setImage:[UIImage imageNamed:@"control_collect"] forState:UIControlStateNormal];
        [self stopHeartBeat];
    }
    else
    {
        [_beaconManager connectPeripheral:l_peri];
        [rightButton setImage:[UIImage imageNamed:@"connect_disconnect"] forState:UIControlStateNormal];
        [self keepConnectAction];
    }
}

- (void)searchSMPenAction
{
    // 点击后当前的智慧笔震动一下
    [_beaconManager searchSMPen];
}

- (void)pairSMPenAction
{
    // 发一个心跳包保持连接，和每10s发一个心跳包的一样
    [_beaconManager heartBeatAction];
}


- (void)mp3PlayAction
{
    [_beaconManager playMp3Action:@"0001"];
}

- (void)keepConnectAction
{
    [self stopHeartBeat];
    _heartBeatTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f repeats:YES block:^(NSTimer * _Nonnull timer) {
        [_beaconManager heartBeatAction];
    }];
}

- (void)stopHeartBeat
{
    [_heartBeatTimer invalidate];
    self.heartBeatTimer = nil;
}

#pragma mark
#pragma mark -------- TableView Delegate --------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return _dataArray.count;}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger nowRow = indexPath.row;
    static NSString *identifier = @"HDSMPenSettingVCCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.textLabel.text   = _dataArray[nowRow];
    cell.imageView.image  = [UIImage imageNamed:_iconArray[nowRow]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL isConnect = [[HDDeclare sharedDeclare] isConnected];
    if(isConnect == NO)
    {
        [LCCoolHUD showFailure:@"尚未连接" zoom:NO shadow:NO];
        return;
    }
    
    NSInteger selectRow = indexPath.row;
    NSString *lineStr = _lineArray[selectRow];
    if (lineStr.length > 0)
    {
        if (selectRow == _lineArray.count-3 ) // 工程模式不开放
        {}
        else
        { [self performSegueWithIdentifier:lineStr sender:nil]; }
    }
    
    else
    {
        switch (selectRow) {
            case 0:
                [self searchSMPenAction];
                break;
            case 1:{
                if ([[HDDeclare sharedDeclare] isConnected] == YES)
                { [LCCoolHUD showSuccess:@"已配对" zoom:YES shadow:YES]; }
                else
                    [self connectToSMPenAction];
            }
                break;
            case 5:{
                [self volumeSettingAction];
            }
                break;
            case 7:{
                [self performSegueWithIdentifier:@"HDSMPenSettingVCPushToNFCLine" sender:nil];
            }
                break;

            case 8:{
                [self mp3PlayAction];
            }
                break;
            case 9:{
                HDSyncDataVC *VC = [[HDSyncDataVC alloc]init];
                [self.navigationController pushViewController:VC animated:YES];
            }
                break;
            default:
                break;
        }
        
    }
}

- (void)volumeSettingAction
{}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
